﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：程序主入口
//
//创建标识：齐李平 || 2011-03-24
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using Hmc.IM.Common;


namespace Hmc.IM.Client
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //Timer tick = new Timer();
            //tick.Interval = 5000;
            //tick.Tick += new EventHandler(tick_Tick);
            //tick.Start();
            try
            {
                Application.Run(new LoginForm());
            }
            catch (OutOfMemoryException omex)
            {
                GC.Collect();
                //添加异常日志
                //Recorder.Write(omex);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("系统发生错误,原因:{0}", ex.Message), "信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //添加异常日志
                ////Recorder.Write(ex);
            }
        }

        static void tick_Tick(object sender, EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (sender1, e1) =>
            {
                CheckFile();
            };
            worker.RunWorkerCompleted += (sender1, e1) => { worker.Dispose(); };
            worker.RunWorkerAsync();
            (sender as Timer).Dispose();
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            //添加异常日志
            //Recorder.Write(e.Exception);
            MessageBox.Show(string.Format("应用程序出错,原因:{0}", e.Exception.Message), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 检查更新的虚文件
        /// </summary>
        private static void CheckFile()
        {
            string currentDiectory = Directory.GetCurrentDirectory();
            string[] files = Directory.GetFiles(currentDiectory);
            for (int i = 0; i < files.Length; i++)
            {
                string[] childfile = files[i].Split('\\');
                if (childfile[childfile.Length - 1].StartsWith("~"))
                {
                    try
                    {
                        File.Copy(files[i], currentDiectory + "\\" + childfile[childfile.Length - 1].Replace("~", ""), true);
                        File.Delete(files[i]);
                    }
                    catch(Exception ex){
                        ////Recorder.Write(ex);//写入到错误日志
                        throw ex;
                    };
                }
                continue;
            }
        }
    }
}
